//
//  StatusRetweetedView.swift
//  weibo
//
//  Created by mac on 15/12/20.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit
import SnapKit
//转发微博 转发微博正文 ＋ 配图视图
class StatusRetweetedView: UIView {
    //定义约束属性 保存当前视图的底部约束
    var botttomConstraints: Constraint?
    //定义外部模型属性
    var retweetedStatus: Status? {
        didSet {
            retweetedLable.text = "@" + (retweetedStatus?.user?.name ?? "") + (retweetedStatus?.text ?? "")
            pictureView.imageURLs = retweetedStatus?.imageURLs
        //卸载掉地步约束
            self.botttomConstraints?.uninstall()
            //根据是否有配图视图 重新添加约束
            if let urls = retweetedStatus?.imageURLs where urls.count > 0 {
            //有配图视图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    //添加约束
                    //记录底部约束
                    self.botttomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                })
            }else {
            //没有配图视图
                self.snp_makeConstraints(closure: { (make) -> Void in
                    //添加约束
                    //记录底部约束
                    self.botttomConstraints = make.bottom.equalTo(retweetedLable.snp_bottom).offset(StatusCellMarigin).constraint
                })
            }
        }
        
    }
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        backgroundColor = UIColor(white: 0.9, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置页面 和 布局
    private func setupUI() {
        addSubview(retweetedLable)
        addSubview(pictureView)
        //添加约束
        retweetedLable.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMarigin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLable.snp_bottom).offset(StatusCellMarigin)
            make.left.equalTo(retweetedLable.snp_left)
        }
        //转发微博自动计算行高
                self.snp_makeConstraints { (make) -> Void in
                    //添加约束
                   self.botttomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                }
    }
    //懒加载所有的子视图
    private lazy var retweetedLable: UILabel = UILabel(title: "我是转发微博", color: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMarigin)
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
